{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "text": [
      "OpenCV version: 3.4.2\n"
     ],
     "output_type": "stream"
    }
   ],
   "source": [
    "# Test of feature detection and matching\n",
    "\n",
    "# Import dependence\n",
    "import cv2 \n",
    "print('OpenCV version:', cv2.__version__)\n",
    "\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "#img0 = cv2.imread('../test_data/images_25/0000.png')\n",
    "#img0[:,:,1],img0[:,:,2]=img0[:,:,2],img0[:,:,1]\n",
    "#plt.imshow(img0,'gray'),plt.title('ORIGINAL')\n",
    "#cv2.namedWindow(\"Image test\") \n",
    "#cv2.imshow(\"Image test\", img0) \n",
    "#cv2.waitKey (0)\n",
    "#cv2.destroyAllWindows()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31merror\u001b[0m                                     Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-811246d73e7f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m      7\u001b[0m \u001b[0msurf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mxfeatures2d\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSURF_create\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m400\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m      8\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0;34m(\u001b[0m\u001b[0mkps\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdescs\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msurf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdetectAndCompute\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mgray\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     10\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     11\u001b[0m \u001b[0;31m#print ('Hessian threshold:',surf.)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31merror\u001b[0m: OpenCV(3.4.2) /io/opencv_contrib/modules/xfeatures2d/src/surf.cpp:892: error: (-215:Assertion failed) !_img.empty() && ((imgtype) & ((1 << 3) - 1)) == 0 && (imgcn == 1 || imgcn == 3 || imgcn == 4) in function 'detectAndCompute'\n"
     ],
     "ename": "error",
     "evalue": "OpenCV(3.4.2) /io/opencv_contrib/modules/xfeatures2d/src/surf.cpp:892: error: (-215:Assertion failed) !_img.empty() && ((imgtype) & ((1 << 3) - 1)) == 0 && (imgcn == 1 || imgcn == 3 || imgcn == 4) in function 'detectAndCompute'\n",
     "output_type": "error"
    }
   ],
   "source": [
    "img = cv2.imread('../test_data/images_25/0000.png')\n",
    "#gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n",
    "\n",
    "gray = cv2.imread('../test_data/images_25/0000.png',cv2.IMREAD_GRAYSCALE)\n",
    "\n",
    "# Here I set Hessian Threshold to 400\n",
    "surf = cv2.xfeatures2d.SURF_create(400)\n",
    "\n",
    "(kps, descs) = surf.detectAndCompute(gray, None)\n",
    "\n",
    "#print ('Hessian threshold:',surf.)\n",
    "print(\"# kps: {}, descriptors: {}\".format(len(kps), descs.shape))\n",
    "\n",
    "img_with_kp = cv2.drawKeypoints(img,kps,None,(255,0,0),4)\n",
    "\n",
    "plt.imshow(img_with_kp),plt.show()\n",
    "\n",
    "\n",
    "\n",
    "'''\n",
    "# Create SURF object. You can specify params here or later.\n",
    "# Here I set Hessian Threshold to 400\n",
    "surf = cv2.SURF(400)\n",
    "\n",
    "\n",
    "# Find keypoints and descriptors directly\n",
    "kp, des = surf.detectAndCompute(gray,None)\n",
    "\n",
    "# Check present Hessian threshold\n",
    "print ('Hessian threshold:',surf.hessianThreshold)\n",
    "print ('keypoint number',len(kp))\n",
    "\n",
    "# We set it to some 50000. Remember, it is just for representing in picture.\n",
    "# In actual cases, it is better to have a value 300-500\n",
    "surf.hessianThreshold = 50000\n",
    "\n",
    "# Again compute keypoints and check its number.\n",
    "kp2, des2 = surf.detectAndCompute(gray,None)\n",
    "print ('Hessian threshold:',surf.hessianThreshold)\n",
    "print ('keypoint number for show',len(kp2))\n",
    "\n",
    "img_with_kp = cv2.drawKeypoints(gray,kp2,None,(255,0,0),4)\n",
    "\n",
    "plt.imshow(img_with_kp),plt.show()\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "text": [
      "OpenCV version: 3.4.2\n"
     ],
     "output_type": "stream"
    },
    {
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31merror\u001b[0m                                     Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-4-95b392b2810c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[0mhessian_threshold\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m800\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     15\u001b[0m \u001b[0msurf\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mxfeatures2d\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mSURF_create\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mhessian_threshold\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0;34m(\u001b[0m\u001b[0mkps1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdescs1\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msurf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdetectAndCompute\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     17\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'[Img 1] # kps: {}, descriptors: {}'\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkps1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdescs1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     18\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mkps2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdescs2\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msurf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdetectAndCompute\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31merror\u001b[0m: OpenCV(3.4.2) /io/opencv_contrib/modules/xfeatures2d/src/surf.cpp:892: error: (-215:Assertion failed) !_img.empty() && ((imgtype) & ((1 << 3) - 1)) == 0 && (imgcn == 1 || imgcn == 3 || imgcn == 4) in function 'detectAndCompute'\n"
     ],
     "ename": "error",
     "evalue": "OpenCV(3.4.2) /io/opencv_contrib/modules/xfeatures2d/src/surf.cpp:892: error: (-215:Assertion failed) !_img.empty() && ((imgtype) & ((1 << 3) - 1)) == 0 && (imgcn == 1 || imgcn == 3 || imgcn == 4) in function 'detectAndCompute'\n",
     "output_type": "error"
    }
   ],
   "source": [
    "# Test of feature detection and matching\n",
    "# Import dependence\n",
    "import cv2 \n",
    "print('OpenCV version:', cv2.__version__)\n",
    "import numpy as np\n",
    "from matplotlib import pyplot as plt\n",
    "\n",
    "\n",
    "# Import Images\n",
    "img1 = cv2.imread('../test_data/images_25/0000.png',cv2.IMREAD_GRAYSCALE)\n",
    "img2 = cv2.imread('../test_data/images_25/0001.png',cv2.IMREAD_GRAYSCALE)\n",
    "\n",
    "# Detect keypoints and extract the SURF descriptors\n",
    "hessian_threshold=800\n",
    "surf = cv2.xfeatures2d.SURF_create(hessian_threshold)\n",
    "(kps1, descs1) = surf.detectAndCompute(img1, None)\n",
    "print('[Img 1] # kps: {}, descriptors: {}'.format(len(kps1), descs1.shape))\n",
    "(kps2, descs2) = surf.detectAndCompute(img2, None)\n",
    "print(\"[Img 2] # kps: {}, descriptors: {}\".format(len(kps2), descs2.shape))\n",
    "\n",
    "# Brute-Force Matching\n",
    "bf = cv2.BFMatcher(cv2.NORM_L2, crossCheck=True)\n",
    "matches = bf.match(descs1, descs2)\n",
    "matches = sorted(matches, key = lambda x:x.distance, reverse=False) # small to big\n",
    "\n",
    "matching_result = cv2.drawMatches(img1, kps1, img2, kps2, matches[ :50: ], None, flags=2)\n",
    "\n",
    "# Show the result\n",
    "img_kp_1 = cv2.drawKeypoints(img1, kps1, None,(0,0,255),4)\n",
    "img_kp_2 = cv2.drawKeypoints(img2, kps2, None,(255,0,0),4)\n",
    "\n",
    "cv2.imshow(\"Img1\", img_kp_1)\n",
    "cv2.imshow(\"Img2\", img_kp_2)\n",
    "cv2.imshow(\"Matching result\", matching_result)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "pycharm": {
     "is_executing": false
    }
   },
   "outputs": [],
   "source": [
    "# MLESAC\n",
    "# instead of counting the inlier number\n",
    "# we weight the inliers with the distance to the line\n",
    "# affine transformation \n",
    "# use RANSAC to find the good initial guess of the group of the correspondences.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "text": [
      "1\n2\n3\n4\n"
     ],
     "output_type": "stream"
    }
   ],
   "source": [
    "list1=[1,2,3,4]\n",
    "\n",
    "for i in list1:\n",
    "    print(i)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n",
     "is_executing": false
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# Crtl+/ multi-line comments\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "source": [],
    "metadata": {
     "collapsed": false
    }
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}